/**
 * Copyright (C), 2020-2020,贵州铭明网络科技有限公司
 * FileName: PermissionsInfoController
 * Author:   杨朝湖
 * Date:     2020/12/8 10:05
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.mm.controller;

import com.mm.bo.InsertPermAdminBo;
import com.mm.bo.UpdatePermAdminBo;
import com.mm.exception.CustomException;
import com.mm.service.PermissionsInfoService;
import com.mm.util.GetHeadUtil;
import com.mm.util.ResultUtil;
import com.mm.vo.PermAdminMenuVo;
import com.mm.vo.PermMenuVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author Gym
 * @create 2020/12/8
 * @since 1.0.0
 */
@Api(tags = "用于权限管理的接口")
@RestController
public class PermissionsInfoController {
    @Resource
    private PermissionsInfoService permissionsInfoService;

    @ApiOperation(value = "查询所有的菜单列表信息", notes = "查询所有的菜单列表信息", httpMethod = "GET")
    @GetMapping("/v1/perm/queryMenuList")
    public ResultUtil<Object> queryMenuList() {
        List<PermMenuVo> permMenuVos = permissionsInfoService.queryMenuList();
        return ResultUtil.success(permMenuVos);
    }

    @ApiOperation(value = "给管理员授权", notes = "给管理员授权", httpMethod = "POST")
    @PostMapping("/v1/perm/insertPremInfo")
    public ResultUtil<Object> insertPremInfo(@RequestBody @Valid InsertPermAdminBo insertPermAdminBo) {
        insertPermAdminBo.setToken(GetHeadUtil.getHeadToken());
        Integer row = permissionsInfoService.insertPremInfo(insertPermAdminBo);
        if (row < 1) {
            throw new CustomException("授权失败");
        }
        return ResultUtil.success(null);
    }

    @ApiOperation(value = "管理员登录成功之后返回菜单列表", notes = "管理员登录成功之后返回菜单列表", httpMethod = "GET")
    @GetMapping("/v1/perm/queryAdminMenuList")
    public ResultUtil<Object> queryAdminMenuList() {
        List<PermAdminMenuVo> permAdminMenuVos = permissionsInfoService.queryAdminMenuList(GetHeadUtil.getHeadToken());
        return ResultUtil.success(permAdminMenuVos);
    }

    @ApiOperation(value = "修改管理员的权限信息", notes = "修改管理员的权限信息", httpMethod = "POST")
    @PostMapping("/v1/perm/updatePremInfo")
    public ResultUtil<Object> updatePremInfo(@RequestBody @Valid UpdatePermAdminBo updatePermAdminBo) {
        updatePermAdminBo.setToken(GetHeadUtil.getHeadToken());
        Integer row = permissionsInfoService.updatePremInfo(updatePermAdminBo);
        if (row < 1) {
            throw new CustomException("修改失败");
        }
        return ResultUtil.success(null);
    }
}